/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property;

import com.inspur.edp.cef.entity.accessor.base.ReadonlyDataException;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.BaseTypePropGenerator;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.ThrowStatement;

public class ReadonlyAccessorPropertyGenerator extends BaseTypePropGenerator {
	private MediatePropertyInfo pi;
	private boolean isExplicit;


//ORIGINAL LINE: private ReadonlyAccessorPropertyGenerator(CefPropertyInfo pi, string interfaceName = null)
	private ReadonlyAccessorPropertyGenerator(MediatePropertyInfo pi, String interfaceName) {
		super(pi, pi.getPropertyName() + "AccessorField");
		this.pi = pi;
	}

	private ReadonlyAccessorPropertyGenerator(MediatePropertyInfo pi, String interfaceName, boolean convertToUpperCase) {
		super(pi, pi.getPropertyName() + "AccessorField", convertToUpperCase);
		this.pi = pi;
	}

	public static ReadonlyAccessorPropertyGenerator createExplicit(MediatePropertyInfo pi) {
//		if (pi.DeclaringType == null || !pi.DeclaringType.IsInterface)
//		{
//			throw new IllegalArgumentException("pi");
//		}
		ReadonlyAccessorPropertyGenerator tempVar = new ReadonlyAccessorPropertyGenerator(pi, "");
		tempVar.isExplicit = true;
		return tempVar;
	}

	public static ReadonlyAccessorPropertyGenerator createImplicit(MediatePropertyInfo pi) {
//		if (pi.DeclaringType == null || !pi.DeclaringType.IsInterface)
//		{
//			throw new IllegalArgumentException("pi");
//		}
		return new ReadonlyAccessorPropertyGenerator(pi, "");
	}

	public static ReadonlyAccessorPropertyGenerator createImplicit(MediatePropertyInfo pi, boolean convertToUpperCase) {
		return new ReadonlyAccessorPropertyGenerator(pi, "", convertToUpperCase);
	}


	@Override
	protected void doInitialize() {
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
		ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getSetterAccessModifier() {
		ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

//
//	@Override
//	protected java.util.ArrayList<PropertyAccessModifier> getAccessModifier() => isExplicit ? new java.util.ArrayList<PropertyAccessModifier>() : new java.util.ArrayList<PropertyAccessModifier>(java.util.Arrays.asList(new PropertyAccessModifier[] { PropertyAccessModifier.Public }));


	private String propName() {
		return pi.getPropertyName();
	}


	@Override
	protected Block buildGetMethodBody() {
		Block block = ast.newBlock();
		ReturnStatement returnStatement = ast.newReturnStatement();
		buildReturnStatementExpression(returnStatement);
		block.statements().add(returnStatement);
		return block;
	}

	private void buildReturnStatementExpression(ReturnStatement returnStatement) {
		MethodInvocation methodInvocation1 = ast.newMethodInvocation();

		MethodInvocation methodInvocationExpression = ast.newMethodInvocation();
		methodInvocationExpression.setName(ast.newSimpleName(InnerDataGeneratpr.getgetterMethodName()));
		methodInvocation1.setExpression(methodInvocationExpression);
		methodInvocation1.setName(ast.newSimpleName("get" + propName()));

		returnStatement.setExpression(methodInvocation1);
	}

	@Override
	protected Block buildSetMethodBody() {
		Block block = ast.newBlock();
		ThrowStatement throwStatement = ast.newThrowStatement();
		ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
		classInstanceCreation.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(ReadonlyDataException.class).getType(ast));
		throwStatement.setExpression(classInstanceCreation);
		block.statements().add(throwStatement);
		return block;
	}

//	@Override
//	protected java.lang.Class getExplicitInterfaceType()
//	{
//		return pi.DeclaringType;
//	}
//
//	@Override
//	protected boolean isExplicit()
//	{
//		return isExplicit;
//	}
}
